import { CHECC_API_CMS } from "@/constants/proxy";
import axios from "axios";
import { getToken } from "@/utils/auth";
import { ElMessage } from "element-plus";

//定义导出统一的方法
export function exportMethod(data) {
  axios({
    method: data.method,
    url: CHECC_API_CMS + data.url,
    responseType: "blob",
    headers: {
      Authorization: "Bearer " + getToken(),
    },
    data: data.data,
  }).then((res) => {
    const blob = new Blob([res.data], { type: "application/vnd.ms-excel" });
    const link = document.createElement("a");
    link.style.display = "none";
    link.href = URL.createObjectURL(blob);
    link.download = data.fileName; //下载的文件名
    document.body.appendChild(link);
    link.click();
    URL.revokeObjectURL(link.href); // 释放URL 对象
    document.body.removeChild(link);
    if (data.fileName == '用户导入模板') {
      ElMessage.success({
        message: "下载成功",
        type: "success",
      });
    } else {
      ElMessage.success({
        message: "导出成功",
        type: "success",
      });
    }

  });
}
